package org.liushengpei.com.controller;

import org.liushengpei.com.domain.User;
import org.liushengpei.com.domain.vo.UserVO;
import org.liushengpei.com.service.IUserService;
import org.liushengpei.com.util.result.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户控制层
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 用户登录
     *
     * @param account  账号
     * @param password 密码
     */
    @PostMapping(value = "/userLogin")
    public ResultData userLogin(@RequestParam("account") String account,
                                @RequestParam(value = "password") String password) {
        UserVO userVO = userService.userLogin(account, password);
        if (userVO == null) {
            return ResultData.fail();
        }
        return ResultData.success(userVO);
    }

    /**
     * 用户注册
     *
     * @param account 电话号码
     * @param onePwd  第一次输入密码
     * @param twoPwd  第二次输入密码
     */
    @PostMapping(value = "/userRegister")
    public ResultData userRegister(@RequestParam(value = "account") String account,
                                   @RequestParam(value = "onePwd") String onePwd,
                                   @RequestParam(value = "twoPwd") String twoPwd) {
        String msg = userService.registerUser(account, onePwd, twoPwd);
        return ResultData.success(msg);
    }

    /**
     * 忘记密码
     *
     * @param user 用户信息
     */
    @PostMapping(value = "/forgetPassword")
    public ResultData forgetPassword(@RequestBody User user) {
        return ResultData.success();
    }

    /**
     * 修改密码
     *
     * @param id     用户id
     * @param newPwd 新密码
     * @param oldPwd 旧密码
     */
    @PostMapping(value = "/updatePassword")
    public ResultData updatePassword(@RequestParam(value = "id") String id,
                                     @RequestParam(value = "oldPwd") String oldPwd,
                                     @RequestParam(value = "newPwd") String newPwd) {
        String msg = userService.updatePassword(id, oldPwd, newPwd);
        return ResultData.success(msg);
    }


    /**
     * 补充个人信息
     *
     * @param user 用户信息
     */
    @PostMapping(value = "/addMineInfo")
    public ResultData addMineInfo(@RequestBody User user) {
        String info = userService.addMineInfo(user);
        return ResultData.success(info);
    }

    /**
     * 上传用户头像
     *
     * @param file 文件
     */
    @PostMapping(value = "/uploadHeadImg")
    public ResultData uploadHeadImg(@RequestParam(value = "file") MultipartFile file) {
        String fileUrl = userService.uploadUserHeadFile(file);
        return ResultData.success(fileUrl);
    }

    /**
     * 获取用户详细信息
     *
     * @param id 用户id
     */
    @GetMapping(value = "/getUserDetail")
    public ResultData getUserDetail(@RequestParam(value = "id") String id) {
        return ResultData.success(userService.getUserDetail(id));
    }

    /**
     * 发送手机验证码
     *
     * @param phone 手机号
     */
    @GetMapping(value = "/sendPhoneCode")
    public ResultData sendPhoneCode(@RequestParam(value = "phone") String phone) {
        return ResultData.success(userService.getPhoneCode(phone));
    }

    /**
     * 发送邮箱验证码
     *
     * @param email 电子邮箱
     */
    @GetMapping(value = "/sendEmailCode")
    public ResultData sendEmailCode(@RequestParam(value = "email") String email) {
        return ResultData.success(userService.getEmailCode(email));
    }

}
